//
//  Today_debris_ContentTableViewCell.m
//  Time
//
//  Created by dllo on 16/7/19.
//  Copyright © 2016年 司昌宝. All rights reserved.
//

#import "Today_debris_ContentTableViewCell.h"
#import "UserinfoView.h"
#import "SCB_Today_Debris_ContentModel.h"

@interface Today_debris_ContentTableViewCell ()
/**
 *  用户视图(头像,时间,用户名)
 */
@property (nonatomic,retain) UserinfoView *userinfoView;
/**
 *  碎片 图
 */
@property (nonatomic,retain) UIImageView *debrisImageView;
/**
 *  文本
 */
@property (nonatomic,retain) UILabel *debrisLabel;
/**
 *  音乐视图
 */
@property (nonatomic,retain) UIView *debrisMusicView;
/**
 *  分享按钮
 */
@property (nonatomic,retain) UIButton *shareButton;
/**
 *  评论按钮
 */
@property (nonatomic,retain) UIButton *commentsButton;
/**
 *  收藏/喜爱,按钮
 */
@property (nonatomic,retain) UIButton *favourButton;

@end

@implementation Today_debris_ContentTableViewCell

-(void)dealloc{
    [_userinfoView release];
    [_model release];
    [_debrisImageView release];
    [_debrisLabel release];
    [_debrisMusicView release];
    [_shareButton release];
    [_commentsButton release];
    [_favourButton release];
    
    [super dealloc];
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
//        self.contentView.backgroundColor = [UIColor yellowColor];
        
        self.userinfoView = [[UserinfoView alloc] init];
        [self.contentView addSubview:self.userinfoView];
        [_userinfoView release];
        
        self.debrisImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:self.debrisImageView];
        [_debrisImageView release];
        
        self.debrisLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.debrisLabel];
        [_debrisLabel release];
        
        self.debrisMusicView = [[UIView alloc] init];
        [self.contentView addSubview:self.debrisMusicView];
        [_debrisMusicView release];
        
        self.favourButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:self.favourButton];
        [self.favourButton addTarget:self action:@selector(favourButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_favourButton release];
        
        self.commentsButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:self.commentsButton];
        [self.commentsButton addTarget:self action:@selector(commentsButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_commentsButton release];
        
        self.shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:self.shareButton];
        [self.shareButton addTarget:self action:@selector(shareButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_shareButton release];
    }
    return self;
}

-(void)favourButtonAction:(UIButton *)button{
    NSLog(@"收藏");
}

-(void)commentsButtonAction:(UIButton *)button{
    NSLog(@"评论");
}

-(void)shareButtonAction:(UIButton *)button{
    NSLog(@"分享");
}

-(void)layoutSubviews{
    
    [self.userinfoView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(10);
        make.right.equalTo(self.contentView.mas_right).offset(-10);
        make.top.equalTo(self.contentView.mas_top).offset(10);
        make.height.mas_equalTo(40);
    }];
    
    self.debrisImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.debrisImageView.clipsToBounds = YES;
    if (![self.model.coverimg isEqualToString:@""]){
        [self.debrisImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).offset(0);
            make.right.equalTo(self.contentView.mas_right).offset(0);
            make.top.equalTo(self.userinfoView.mas_bottom).offset(10);
            make.height.mas_equalTo(SWIDTH);
        }];
    } else {
        [self.debrisImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).offset(0);
            make.right.equalTo(self.contentView.mas_right).offset(0);
            make.top.equalTo(self.userinfoView.mas_bottom).offset(10);
            make.height.mas_equalTo(0);
        }];
    }
    
    self.debrisLabel.textColor = [UIColor grayColor];
    self.debrisLabel.font = [UIFont systemFontOfSize:15.0];
    self.debrisLabel.numberOfLines = 0;
    [self.debrisLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(10);
        make.right.equalTo(self.contentView.mas_right).offset(-10);
        make.top.equalTo(self.debrisImageView.mas_bottom).offset(10);
        make.height.mas_equalTo([Tools getTextHeight:self.model.text withWidth:SWIDTH - 20 withFontSize:15.0] + 10);
    }];
    
    self.debrisMusicView.backgroundColor = [UIColor redColor];
//    if (![self.model.songid isEqualToString:@""]) {
//        [self.debrisMusicView mas_remakeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(self.contentView.mas_left).offset(10);
//            make.right.equalTo(self.contentView.mas_right).offset(-10);
//            make.top.equalTo(self.debrisLabel.mas_bottom).offset(10);
//            make.height.mas_equalTo(60);
//        }];
//    } else {
        [self.debrisMusicView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).offset(10);
            make.right.equalTo(self.contentView.mas_right).offset(-10);
            make.top.equalTo(self.debrisLabel.mas_bottom).offset(10);
            make.height.mas_equalTo(0);
        }];
//    }

    [self.favourButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView.mas_right).offset(-10);
        make.width.mas_equalTo(SWIDTH * 0.05);
        make.top.equalTo(self.debrisMusicView.mas_bottom).offset(10);
        make.height.mas_equalTo(SWIDTH * 0.05);
    }];
    
    [self.commentsButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.favourButton.mas_left).offset(-20);
        make.width.mas_equalTo(SWIDTH * 0.05);
        make.top.equalTo(self.debrisMusicView.mas_bottom).offset(10);
        make.height.mas_equalTo(SWIDTH * 0.05);
    }];
    
    [self.shareButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.commentsButton.mas_left).offset(-20);
        make.width.mas_equalTo(SWIDTH * 0.05);
        make.top.equalTo(self.debrisMusicView.mas_bottom).offset(10);
        make.height.mas_equalTo(SWIDTH * 0.05);
    }];
}



-(void)setModel:(SCB_Today_Debris_ContentModel *)model{
    if (_model != model){
        [_model release];
        _model = [model retain];
    }
    self.userinfoView.scbUserinfo = model.scbUserinfo;
    self.userinfoView.pubdate = model.addtime;
    
    [self.debrisImageView setImageWithURL:[NSURL URLWithString:model.coverimg] placeholderImage:nil];
    
    self.debrisLabel.text = model.text;
    
//    self.debrisMusicView.backgroundColor = [UIColor grayColor];
    
    [self.favourButton setImage:[UIImage imageNamed:@"收藏 (3)"] forState:UIControlStateNormal];
    [self.commentsButton setImage:[UIImage imageNamed:@"评论 (4)"] forState:UIControlStateNormal];
    [self.shareButton setImage:[UIImage imageNamed:@"分享"] forState:UIControlStateNormal];
}



@end
